package com.training.spring.controller;


import com.training.spring.model.MessageInfo;
import com.training.spring.service.MessageService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    @RequestMapping("/publish")
    // @RequestBody:从请求的正文(Body)中解析数据(JSON)并映射到info
    // 如果不加该注释,无法正确解析并映射数据
    public Boolean publish(@RequestBody MessageInfo info) {
        if (!StringUtils.hasLength(info.getFrom())
                || !StringUtils.hasLength(info.getTo())
                || !StringUtils.hasLength(info.getMessage())) {
            return false;
        }
        messageService.insertMessage(info);
        return true;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.getMessage();
    }
}
